﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
//
using System.Web.Services;
using DDSC.OracleADO;
using System.Collections;
using System.Text;
using System.Data;

using System.Net;
using System.Drawing.Imaging;
using System.Net.Mime;
using System.Net.Mail;



public partial class krj_tracking_detail : BasePermissionPage
{
    //維護動作
    public string Mode { get; set; }
    
    //搜尋SQL
    [WebMethod]
    public static Object GetCorrection(string DETAIL_NO, string ITEM_NO, PageInfo p_PageInfo)
    {
        Object results = new Object();
        try
        {
            dbAccess m_dba = new dbAccess();
            StringBuilder m_sb = new StringBuilder();
            ArrayList m_arrParam = new ArrayList();
            m_sb.Append("select F21524 || ' ' || B.DEPT_NAME RECEIVED_DEPT,F21525 SHIPPER_ID,F21526 SHIPPER, F21527 RECEIVER,F21537 || ' ' || F21538 || ' ' || F21539 || ' ' || F21540 || ' ' || F21541 || ' ' || F21542 CORRECT_REASON,F21501 CORRECTION_NO,F21582 || ' ' || C.USER_NAME CORRECT_ID ");
            m_sb.Append(" ,F21502 DETAIL_NO ,F21503 ITEM_NO");
            m_sb.Append(" from as400_zdf215 A");
            m_sb.Append(" left join mis_dept_ap B on A.F21524 = B.DEPT_ID ");
            m_sb.Append(" left join MIS_USER_AP C on A.F21582 = C.USER_ID ");
            
            m_sb.Append(" Where 1=1 ");
            if (DETAIL_NO != "")
            {
                m_sb.Append(" AND F21502 =:DETAIL_NO ");
                m_sb.Append(" AND F21503 =:ITEM_NO ");
                m_arrParam.Add(m_dba.GetOracleParam(":DETAIL_NO", DETAIL_NO));
                m_arrParam.Add(m_dba.GetOracleParam(":ITEM_NO", ITEM_NO));
            }
            results = ADOGridHandler(m_sb, m_arrParam, p_PageInfo);
        }
        catch (Exception ex)
        {
            BasePermissionPage BP = new BasePermissionPage();
            //results = BP.BP_Message;
            BP.ERROR_DBSAVE("krj_tracking_detail.aspx.cs", "GetCorrection", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return (results);

    }

    //搜尋SQL
    [WebMethod]
    public static Object GetTracking(string DETAIL_NO, string ITEM_NO, string History, PageInfo p_PageInfo)
    {
        Object results = new Object();
        try
        {
            dbAccess m_dba = new dbAccess();
            StringBuilder m_sb = new StringBuilder();
            ArrayList m_arrParam = new ArrayList();

            m_sb.Append("select CORRECTION_NO, Z.atype,Z.data_source,Z.btype,Z.tracking_code,Z.DEPT_ID || ' ' || X.DEPT_NAME DEPT_ID,Z.PROCESS_DATE,Z.PROCESS_TIME,Z.PROCESSER,Z.ITEMS_COUNT,Z.SHIPPING_NO as TRANS_NO from  ");
            m_sb.Append(" ( ");
            m_sb.Append(" select F61901 || F61902 CORRECTION_NO ,N'發送登錄' atype, N'A' data_source,N'00' btype ,N'A1' tracking_code,F61906 DEPT_ID,F61974 PROCESS_DATE,F61975 PROCESS_TIME,F61973 PROCESSER,F61925 ITEMS_COUNT,N'' SHIPPING_NO ");
            m_sb.Append(" From (select Z.F61901,Z.F61902,Z.F61903, Z.F61906,Z.F61975,Z.F61973,Z.F61974,Z.F61925 from as400_zdf619 Z  inner join (select F61901 ,F61902,min(F61903) F61903 from as400_zdf619 where F61901 = :SHIPPING_NO_21 and  F61902 = :SHIPPING_NO_22 group by  F61901 ,F61902 ) X on Z.F61901 ||Z.F61902 || Z.F61903 = X.F61901 ||X.F61902 || X.F61903 where Z.F61901 = :SHIPPING_NO_11 and  Z.F61902 = :SHIPPING_NO_12 ) A ");
            m_sb.Append(" union all ");
            m_sb.Append(" SELECT F63101||F63102 CORRECTION_NO , decode(t.rn, 1, N'點貨', 2, N'攜出', 3, N'銷卡', 4, N'存庫') atype, ");
            m_sb.Append(" N'A' data_source, ");
            m_sb.Append(" N'00' btype, ");
            m_sb.Append(" decode(t.rn, 1, N'A2', 2, N'73', 3, F63162, 4, NVL(F63172,' ')) tracking_code, ");
            m_sb.Append(" decode(t.rn, 1, F63106, 2, F63106, 3, F63106, 4, F63106) DEPT_ID, ");
            m_sb.Append(" decode(t.rn, 1, cast(F6315B as nvarchar2(8)), 2, F6318B, 3, F63161, 4, F63171) PROCESS_DATE, ");
            m_sb.Append(" decode(t.rn, 1, cast(F6315C as nvarchar2(8)), 2, F6318C, 3, F6316C, 4, F6317C) PROCESS_TIME, ");
            m_sb.Append(" decode(t.rn, 1, cast(F63155 as nvarchar2(8)), 2, F6318A, 3, F63167, 4, F63177) PROCESSER, ");
            m_sb.Append(" decode(t.rn, 1, (F63119 - F63157), 2, F63187, 3, (F63119 - F63164), 4, F63164) ITEMS_COUNT, ");
            m_sb.Append(" decode(t.rn, 1, N'', 2, F63185, 3, F63165, 4, F63175) SHIPPING_NO ");
            m_sb.Append(" FROM (SELECT a.*, b.rn FROM AS400_ZDF631 a, ");
            m_sb.Append(" (SELECT ROWNUM rn FROM dual CONNECT BY ROWNUM <= 4) b ");
            m_sb.Append(" where exists (select * from (select N'" + DETAIL_NO + "' F21501, N'" + ITEM_NO + "' F21503 from dual union all select F21501,F21503 from as400_ZDF215 Z where F21502 = :DETAIL_NO1 and F21503 = :ITEM_NO1) J where a.F63101 = J.F21501 and a.F63102 = J.F21503  )");
            m_sb.Append(" ) t ");
               
            //CRF90-車機銷卡
            m_sb.Append(" union all ");
            m_sb.Append(" select CR9006 || CR9007 CORRECTION_NO,N'車機銷卡' atype,N'F' data_source,N'11' btype ,CR9008 tracking_code,CR9004 DEPT_ID,CR9001 PROCESS_DATE,CR9002 PROCESS_TIME,N'0' PROCESSER,0 ITEMS_COUNT,CR9005 SHIPPING_NO  ");
            m_sb.Append(" from as400_crf90 a ");
            m_sb.Append(" where exists (select * from (select N'" + DETAIL_NO + "' F21501, N'" + ITEM_NO + "' F21503 from dual union all select F21501,F21503 from as400_ZDF215 Z where F21502 = :DETAIL_NO2 and F21503 = :ITEM_NO2) J where a.CR9006 = J.F21501 and a.CR9007 = J.F21503  )");
            m_sb.Append(" and CR9008 in (N'02', N'07', N'00', N'19', N'20' ,N'21', N'22', N'23', N'24', N'25' ,N'26' ,N'27' ,N'28' ,N'29' ,N'30')");
            m_sb.Append(" and CR9000 between N'21' and N'39' ");
            //as400_CEFA6
            m_sb.Append(" union all ");
            m_sb.Append(" select CEA6B1 CORRECTION_NO,N'' atype,N'F' data_source,N'03' btype ,CEA6B3 tracking_code,CEA6BC DEPT_ID,CEA6B7 PROCESS_DATE,CEA6B8 PROCESS_TIME,CEA6B5 PROCESSER,0 ITEMS_COUNT,CEA6BD SHIPPING_NO  ");
            m_sb.Append(" from as400_CEFA6 a ");
            m_sb.Append(" where exists (select * from (select N'" + DETAIL_NO + ITEM_NO + "' F215Z1 from dual union all select F215Z1 from as400_ZDF215 Z where F21502 = :DETAIL_NO3 and F21503 = :ITEM_NO3) J where a.CEA6B1 = J.F215Z1  )");
            m_sb.Append(" and (CEA6B3 is null or CEA6B3 <> 73) ");
            //as400_htf25
            m_sb.Append(" union all ");
            m_sb.Append(" select HT2502 CORRECTION_NO,N'聯運點貨' atype,N'' data_source,N'00' btype ,N'41' tracking_code,HT2581 DEPT_ID,HT2515 PROCESS_DATE,HT2516 PROCESS_TIME,HT2583 PROCESSER,HT2513 ITEMS_COUNT,N'' SHIPPING_NO  ");
            m_sb.Append(" from as400_htf25 a ");
            m_sb.Append(" where exists (select * from (select N'" + DETAIL_NO + ITEM_NO + "' F215Z1 from dual union all select F215Z1 from as400_ZDF215 Z where F21502 = :DETAIL_NO4 and F21503 = :ITEM_NO4) J where a.HT2502 = J.F215Z1 )");

            m_sb.Append(" union all ");
            m_sb.Append(" select CR8201 CORRECTION_NO,N'' atype,CR8231 data_source,N'00' btype ,CR8202 tracking_code,CR8211 DEPT_ID,CR8212 PROCESS_DATE,CR8213 PROCESS_TIME,CR8214 PROCESSER,to_number(CR8217) ITEMS_COUNT,CR8215 SHIPPING_NO ");
            m_sb.Append(" from as400_crf82 a ");
            m_sb.Append(" where exists (select * from (select N'" + DETAIL_NO + ITEM_NO + "' F215Z1 from dual union all select F215Z1 from as400_ZDF215 Z where F21502 = :DETAIL_NO5 and F21503 = :ITEM_NO5) J where a.CR8201 = J.F215Z1 )");
            m_sb.Append(" and CR8231 in ('K','H') ");

            m_sb.Append(" union all ");
            m_sb.Append(" select HT2702 CORRECTION_NO,N'無板貨件' atype,N'H' data_source,N'00' btype ,N'A4' tracking_code,HT2781 DEPT_ID,HT2715 PROCESS_DATE,HT2716 PROCESS_TIME,HT2783 PROCESSER,HT2713 ITEMS_COUNT,N'' SHIPPING_NO  ");
            m_sb.Append(" from as400_htf27 a inner join  ");
            m_sb.Append(" (select * from (select N'" + DETAIL_NO + ITEM_NO + "' F215Z1 from dual union all select F215Z1 from as400_ZDF215 Z where F21502 = :DETAIL_NO6 and F21503 = :ITEM_NO6)) J on a.HT2702 = J.F215Z1 ");
            //m_sb.Append(" where exists (select * from (select N'" + DETAIL_NO + ITEM_NO + "' F215Z1 from dual union all select F215Z1 from as400_ZDF215 Z where F21502 = :DETAIL_NO6 and F21503 = :ITEM_NO6) J where a.HT2702 = J.F215Z1  )");
            m_sb.Append(" where (HT2732 is null or HT2732 <> '1') ");

            m_sb.Append(" union all ");
            m_sb.Append(" select HT2901 CORRECTION_NO, N'到著盤點' atype,N'H' data_source,N'00' btype ,N'A3' tracking_code,HT2981 DEPT_ID,HT2913 PROCESS_DATE,HT2914 PROCESS_TIME,HT2983 PROCESSER,HT2911 ITEMS_COUNT,N'' SHIPPING_NO   ");
            m_sb.Append(" from as400_htf29 a ");
            m_sb.Append(" where exists (select * from (select N'" + DETAIL_NO + ITEM_NO + "' F215Z1 from dual union all select F215Z1 from as400_ZDF215 Z where F21502 = :DETAIL_NO7 and F21503 = :ITEM_NO7) J where a.HT2901 = J.F215Z1 )");

            //CRF18 - 各單位貨況記錄檔
            m_sb.Append(" union all ");
            m_sb.Append(" select CR1801 || CR1802 CORRECTION_NO, N'配貨攜出' atype,N'F' data_source,N'00' btype ,N'73' tracking_code,STATION_ID DEPT_ID,CR1805 PROCESS_DATE,CR1806 PROCESS_TIME,CR1808 PROCESSER,CR1810 ITEMS_COUNT,CR1809 SHIPPING_NO  ");
            m_sb.Append(" from AS400_CRF18 a ");
            m_sb.Append(" where exists (select * from (select N'" + DETAIL_NO + "' F21501, N'" + ITEM_NO + "' F21503 from dual union all select F21501,F21503 from as400_ZDF215 Z where F21502 = :DETAIL_NO8 and F21503 = :ITEM_NO8) J where a.CR1801 = J.F21501 and a.CR1802 = J.F21503  )");
         
            m_sb.Append(" ) Z left join MIS_DEPT_AP X on Z.DEPT_ID = X.DEPT_ID ");
            m_sb.Append(" where Z.tracking_code is not null and Z.PROCESS_TIME <> 0 ");
            m_sb.Append(" Order by Z.PROCESS_DATE desc,Z.PROCESS_TIME desc");



            if (DETAIL_NO != "")
            {
                string SHIPPING_NO_1 = "", SHIPPING_NO_2 = "";
                SHIPPING_NO_1 = (DETAIL_NO + ITEM_NO).Substring(0,10);
                SHIPPING_NO_2 = ITEM_NO.Substring(3,1);
                m_arrParam.Add(m_dba.GetOracleParam(":SHIPPING_NO_11", SHIPPING_NO_1));
                m_arrParam.Add(m_dba.GetOracleParam(":SHIPPING_NO_12", SHIPPING_NO_2));
                m_arrParam.Add(m_dba.GetOracleParam(":SHIPPING_NO_21", SHIPPING_NO_1));
                m_arrParam.Add(m_dba.GetOracleParam(":SHIPPING_NO_22", SHIPPING_NO_2));
                m_arrParam.Add(m_dba.GetOracleParam(":DETAIL_NO1", DETAIL_NO));
                m_arrParam.Add(m_dba.GetOracleParam(":ITEM_NO1", ITEM_NO));
                
                m_arrParam.Add(m_dba.GetOracleParam(":DETAIL_NO2", DETAIL_NO));
                m_arrParam.Add(m_dba.GetOracleParam(":ITEM_NO2", ITEM_NO));
                m_arrParam.Add(m_dba.GetOracleParam(":DETAIL_NO3", DETAIL_NO));
                m_arrParam.Add(m_dba.GetOracleParam(":ITEM_NO3", ITEM_NO));
                m_arrParam.Add(m_dba.GetOracleParam(":DETAIL_NO4", DETAIL_NO));
                m_arrParam.Add(m_dba.GetOracleParam(":ITEM_NO4", ITEM_NO));
                m_arrParam.Add(m_dba.GetOracleParam(":DETAIL_NO5", DETAIL_NO));
                m_arrParam.Add(m_dba.GetOracleParam(":ITEM_NO5", ITEM_NO));
                m_arrParam.Add(m_dba.GetOracleParam(":DETAIL_NO6", DETAIL_NO));
                m_arrParam.Add(m_dba.GetOracleParam(":ITEM_NO6", ITEM_NO));
                m_arrParam.Add(m_dba.GetOracleParam(":DETAIL_NO7", DETAIL_NO));
                m_arrParam.Add(m_dba.GetOracleParam(":ITEM_NO7", ITEM_NO));
                m_arrParam.Add(m_dba.GetOracleParam(":DETAIL_NO8", DETAIL_NO));
                m_arrParam.Add(m_dba.GetOracleParam(":ITEM_NO8", ITEM_NO));
            }
            if (History.ToLower() == "true")
            {
                m_sb.Replace("AS400_ZDF631", "AS400_ZDF631Z");
            }
            results = ADOGridHandler(m_sb, m_arrParam, p_PageInfo);
        }
        catch (Exception ex)
        {
            //results = ex.Message;
            BasePermissionPage BP = new BasePermissionPage();
            //results = BP.BP_Message;
            BP.ERROR_DBSAVE("krj_tracking_detail.aspx.cs", "GetTracking", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return (results);

    }
    //搜尋SQL
    [WebMethod]
    public static Object GetChecking(string DETAIL_NO, string ITEM_NO, PageInfo p_PageInfo)
    {
        Object results = new Object();
        try
        {
            dbAccess m_dba = new dbAccess();
            StringBuilder m_sb = new StringBuilder();
            ArrayList m_arrParam = new ArrayList();

            m_sb.Append("select C.DEPT_NAME CHECKING_DEPT,D.DEPT_NAME REPLY_DEPT,A.F26210 ITEM_NO,A.F26201 CHECKING_NO,A.F26202 CHECKING_SEQ, A.F26205 CHECKING_DATE, B.F26404 REPLY_REASON,A.F26223 REPLY  ");
            m_sb.Append(" from AS400_ZDF262 A left join (select F26401,F26402,F26404 From AS400_ZDF264 Z inner join (select distinct F26201,F26202 from AS400_ZDF262  where F26209 = :DETAIL_NO_1 and F26210 = :ITEM_NO_1 ) X on X.F26201 = Z.F26401 and X.F26202 = Z.F26402 where F26403 = '1') B on A.F26201 = B.F26401 and A.F26202 = B.F26402 ");
            m_sb.Append(" left join MIS_DEPT_AP C on A.F26203 = C.DEPT_ID ");
            m_sb.Append(" left join MIS_DEPT_AP D on A.F26207 = D.DEPT_ID ");

            m_sb.Append(" where A.F26209 = :DETAIL_NO_2 and A.F26210 = :ITEM_NO_2  ");
            m_arrParam.Add(m_dba.GetOracleParam(":DETAIL_NO_1", DETAIL_NO));
            m_arrParam.Add(m_dba.GetOracleParam(":ITEM_NO_1", ITEM_NO));
            m_arrParam.Add(m_dba.GetOracleParam(":DETAIL_NO_2", DETAIL_NO));
            m_arrParam.Add(m_dba.GetOracleParam(":ITEM_NO_2", ITEM_NO));

            results = ADOGridHandler(m_sb, m_arrParam, p_PageInfo);
        }
        catch (Exception ex)
        {
            //results = ex.Message;
            BasePermissionPage BP = new BasePermissionPage();
            //results = BP.BP_Message;
            BP.ERROR_DBSAVE("krj_tracking_detail.aspx.cs", "GetChecking", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return (results);

    }

    //搜尋SQL-發送資料
    [WebMethod]
    public static Object Getmislanded(string CORRECT_NO, PageInfo p_PageInfo)
    {
        Object results = new Object();
        try
        {
            dbAccess m_dba = new dbAccess();
            StringBuilder m_sb = new StringBuilder();
            ArrayList m_arrParam = new ArrayList();

            m_sb.Append(" select SA2114 scan_date,SA2115 scan_time ,SA2102 || ' ' || B.DEPT_NAME scan_dept ,SA2113 scan_count ,SA2111 || ' ' || C.USER_NAME CHECK_USER_ID ");
            //m_sb.Append(" ,F63153 LINE_TYPE , KD1102 LINE_NAME  ");
            m_sb.Append(" from as400_SAF21 A left join MIS_DEPT_AP B on A.SA2102 = B.DEPT_ID ");
            m_sb.Append(" left join MIS_USER_AP C on A.SA2111 = C.USER_ID ");
            //m_sb.Append(" left join AS400_KDF11 D on A.F63153 = D.KD1101  ");

            m_sb.Append(" where SA2101 =:CORRECT_NO ");
            m_arrParam.Add(m_dba.GetOracleParam(":CORRECT_NO", CORRECT_NO));

            results = ADOGridHandler(m_sb, m_arrParam, p_PageInfo);
        }
        catch (Exception ex)
        {
            //results = ex.Message;
            BasePermissionPage BP = new BasePermissionPage();
            //results = BP.BP_Message;
            BP.ERROR_DBSAVE("krj_tracking_detail.aspx.cs", "GetChecking", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return (results);

    }

    
    [WebMethod]
    public static Object selectedrow(string DETAIL_NO, string CORRECT_NO, string ITEM_NO, string ORG_SHIPPING_NO, string sTYPE, string History)
    {
        dbAccess m_dba = new dbAccess();
        Object results = new Object();
        
        try
        {
            DataSet ds = null;
            krj_tracking_detail detail = new krj_tracking_detail();
            if (sTYPE != "NEW" && detail.chk_F631(CORRECT_NO, ITEM_NO,History))
            {
                DETAIL_NO = CORRECT_NO;
            }
            ds = detail.DBINT(DETAIL_NO, ITEM_NO, CORRECT_NO, ORG_SHIPPING_NO,History);
            if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "000" && ds.Tables["zdf631"].Rows.Count == 1)
            {
                results = m_dba.RowsToDictionary(ds.Tables["zdf631"], "1", "1");
                //m_dba.ToJson(ds,"1","1"
            }
            else
            {
                if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "100")
                {
                    results = "error_no讀取無資料！"  ;
                }
                else
                {
                    results = "error資料讀取錯誤！" + ds.Tables[0].Rows[0]["LogMessage"];
                }
            }
            
        }
        catch (Exception ex)
        {
            //results = "error" + ex.Message;
            BasePermissionPage BP = new BasePermissionPage();
            results = "error" + BP.BP_Message;
            BP.ERROR_DBSAVE("krj_tracking_detail.aspx.cs", "selectedrow", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return results;
    }

    [WebMethod]
    public static Object selectchecking(string CHECKING_NO, string CHECKING_SEQ)
    {
        dbAccess m_dba = new dbAccess();
        Object results = new Object();

        try
        {
            DataSet ds = null;
            krj_tracking_detail detail = new krj_tracking_detail();

            ds = detail.chk_F262(CHECKING_NO, CHECKING_SEQ);
            if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "000" && ds.Tables["zdf262"].Rows.Count == 1)
            {
                results = m_dba.RowsToDictionary(ds.Tables["zdf262"], "1", "1");
                //m_dba.ToJson(ds,"1","1"
            }
            else
            {
                if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "100")
                {
                    results = "error_no讀取無資料！";
                }
                else
                {
                    results = "error資料讀取錯誤！" + ds.Tables[0].Rows[0]["LogMessage"];
                }
            }
        }
        catch (Exception ex)
        {
            //results = "error" + ex.Message;
            BasePermissionPage BP = new BasePermissionPage();
            results = BP.BP_Message;
            BP.ERROR_DBSAVE("krj_tracking_detail.aspx.cs", "selectchecking", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return results;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            
            string srcImageUrl;
            if (!Page.IsPostBack)
            {
                krj_tracking previous = PreviousPage as krj_tracking;
                string mode = (string)Request["mode"];
                
                if (PreviousPage != null)
                {
                    
                    mode = previous.Mode;
                    hidHistory.Value = previous.History.ToString();

                    //從前一頁帶入KEY資料
                    if (mode != "Insert")
                    {
                        lblDetail_NO.Text = previous.g_GridKey["DETAIL_NO"].ToString();

                    }
                }


                if (mode != "Insert")
                {
                    string DETAIL_NO = "", ITEM_NO = "", CORRECT_NO = "", ORG_SHIPPING_NO="";
                    DETAIL_NO = previous.g_GridKey["DETAIL_NO"].ToString();
                    ITEM_NO = previous.g_GridKey["ITEM_NO"].ToString();
                    CORRECT_NO = previous.g_GridKey["CORRECT_NO"].ToString();
                    ORG_SHIPPING_NO = previous.g_GridKey["ORG_SHIPPING_NO"].ToString(); //原託運單號
                    hidDetail_NO.Value = DETAIL_NO;
                    hidITEM_NO.Value = ITEM_NO;
                    hidCORRECT_NO.Value = CORRECT_NO;
                    hidORG_SHIPPING_NO.Value = ORG_SHIPPING_NO;
                    DataSet ds = null;
                    Object results = new Object();
                    try
                    {
                        ds = DBINT(DETAIL_NO, ITEM_NO, CORRECT_NO, ORG_SHIPPING_NO, hidHistory.Value);
                        if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "000" && ds.Tables["zdf631"].Rows.Count == 1)
                        {
                            
                            //發到整合查詢
                            lblDetail_NO.Text = ds.Tables["zdf631"].Rows[0]["F63101"].ToString().Trim();    //明細單號
                            lblITEM_NO.Text = ds.Tables["zdf631"].Rows[0]["F63102"].ToString().Trim();    //貨號
                            
                            lblCANCEL.Text = ds.Tables["zdf631"].Rows[0]["f20602_1"].ToString().Trim(); //銷卡
                            lblSTOCK.Text = ds.Tables["zdf631"].Rows[0]["f20602_2"].ToString().Trim(); ; //存庫
                            lblDISPATCH_DATE.Text = ds.Tables["zdf631"].Rows[0]["F63103"].ToString().Trim(); //發送日期
                            lblRECEIVED_DATE.Text = ds.Tables["zdf631"].Rows[0]["F63105"].ToString().Trim(); //到著日期
                            lblDelivery_Note.Text = ds.Tables["zdf631"].Rows[0]["F631B4"].ToString().Trim(); //出貨單號
                            lblDISPATCH_DEPT.Text = ds.Tables["zdf631"].Rows[0]["F21504"].ToString().Trim() + " " + ds.Tables["zdf631"].Rows[0]["DISPATCH_DEPT"].ToString().Trim(); //發送站
                            lblRECEIVED_DEPT.Text = ds.Tables["zdf631"].Rows[0]["F21510"].ToString().Trim() + " " + ds.Tables["zdf631"].Rows[0]["RECEIVED_DEPT"].ToString().Trim(); //到著站
                            lblCORRECT_DATE.Text = ds.Tables["zdf631"].Rows[0]["F21509"].ToString().Trim(); //更正日 ******
                            lblSHIPPING_NO_1.Text = ds.Tables["zdf631"].Rows[0]["F631A1"].ToString().Trim(); //托運單號
                            lblPAYMENT_NO_1.Text = ds.Tables["zdf631"].Rows[0]["F63117"].ToString().Trim(); ; //付款人編號
                            lblCORRECT_NO.Text = ds.Tables["zdf631"].Rows[0]["F21501"].ToString().Trim(); //更正單號 ******
                            lblSHIPPER.Text = ds.Tables["zdf631"].Rows[0]["F21526"].ToString().Trim(); //更正托運人
                            lblRECEIVER.Text = ds.Tables["zdf631"].Rows[0]["F21527"].ToString().Trim(); //更正收貨人
                            lblCORRECT_DEPT.Text = ds.Tables["zdf631"].Rows[0]["F21524"].ToString().Trim() + " " + ds.Tables["zdf631"].Rows[0]["CORRECT_DEPT"].ToString().Trim(); //更正後所站 *****
                            lblORG_PAYMENT.Text = ds.Tables["zdf631"].Rows[0]["F63141"].ToString().Trim(); //原訂代收款 *****
                            lblSPECIFIED_SECTIONS.Text = ds.Tables["zdf631"].Rows[0]["F631C1"].ToString().Trim();  //指送時段
                            lblSPECIFIED_DATE.Text = ds.Tables["zdf631"].Rows[0]["F63107"].ToString().Trim(); //指送日期
                            lblPROJECT.Text = ds.Tables["zdf631"].Rows[0]["F631B3"].ToString().Trim();  //行銷專案
                            lblPAYMENT.Text = ds.Tables["zdf631"].Rows[0]["F21532"].ToString().Trim(); //更正-支付
                            lblSHIPPING_AMOUNT.Text = ds.Tables["zdf631"].Rows[0]["F21533"].ToString().Trim(); //更正-運費
                            lblPAY_AMOUNT.Text = ds.Tables["zdf631"].Rows[0]["F21575"].ToString().Trim(); //更正-報值費
                            lblITEM_COUNT.Text = ds.Tables["zdf631"].Rows[0]["F21529"].ToString().Trim(); //更正-件數
                            lblTRANS_CODE.Text = ds.Tables["zdf631"].Rows[0]["F2157A"].ToString().Trim(); //更正-運選碼
                            lblPAY1.Text = ds.Tables["zdf631"].Rows[0]["F21534"].ToString().Trim(); //更正-接費
                            lblPAY2.Text = ""; //挽工費
                            lblWEIGHT.Text = ds.Tables["zdf631"].Rows[0]["F21530"].ToString().Trim(); //更正-重量
                            lblAMOUNT1.Text = ds.Tables["zdf631"].Rows[0]["F21579"].ToString().Trim(); //更正-報值額
                            lblAMOUNT2.Text = ds.Tables["zdf631"].Rows[0]["F21535"].ToString().Trim(); //更正-送費
                            lblAMOUNT3.Text = ds.Tables["zdf631"].Rows[0]["F21574"].ToString().Trim(); //更正-特服費
                            lblCubic.Text = ds.Tables["zdf631"].Rows[0]["F21531"].ToString().Trim(); //更正-才數
                            lblCODE1.Text = ds.Tables["zdf631"].Rows[0]["F21572"].ToString().Trim(); //更正-特服碼1
                            lblAMOUNT4.Text = ds.Tables["zdf631"].Rows[0]["F21536"].ToString().Trim(); //代款
                            //20130626 Mail
                            //運費小計計算公式為
                            //F63125+F63126+F63127+F63128+F63129+F63130+F63131+F63132+F63133
                            //運費+接費+接捆工費+送費+送捆工費+代款+報值費+特服費+假日費
                            Int32 Totaal_Amount = 0;
                            Totaal_Amount = Convert.ToInt32(lblSHIPPING_AMOUNT.Text) + Convert.ToInt32(lblPAY1.Text) ;
                            Totaal_Amount = Totaal_Amount + Convert.ToInt32(ds.Tables["zdf631"].Rows[0]["F63127"].ToString().Trim()); //接捆工費
                            Totaal_Amount = Totaal_Amount  + Convert.ToInt32(lblAMOUNT2.Text);
                            Totaal_Amount = Totaal_Amount + Totaal_Amount + Convert.ToInt32(ds.Tables["zdf631"].Rows[0]["F63128"].ToString().Trim()); //送捆工費;
                            Totaal_Amount = Totaal_Amount+ Convert.ToInt32(lblAMOUNT4.Text) ;
                            Totaal_Amount = Totaal_Amount+ Convert.ToInt32(lblPAY_AMOUNT.Text) ;
                            Totaal_Amount = Totaal_Amount+ Convert.ToInt32(lblAMOUNT3.Text);
                            Totaal_Amount = Totaal_Amount + Totaal_Amount + Totaal_Amount + Convert.ToInt32(ds.Tables["zdf631"].Rows[0]["F63133"].ToString().Trim()); //假日費
                            lblAMOUNT5.Text = Totaal_Amount.ToString(); //小計
                            lblAddition.Text = ds.Tables["zdf631"].Rows[0]["F631B5"].ToString().Trim(); //加成
                            lblTRANS_USER_ID.Text = ds.Tables["zdf631"].Rows[0]["F61977"].ToString().Trim(); //運輸單
                            lblRECEIVE_USER_ID.Text = ds.Tables["zdf631"].Rows[0]["F61971"].ToString().Trim() + " " + ds.Tables["zdf631"].Rows[0]["F61971_1"].ToString().Trim(); //受付員
                            lblCREATE_USER_ID.Text = ds.Tables["zdf631"].Rows[0]["F61973"].ToString().Trim() + " " + ds.Tables["zdf631"].Rows[0]["F61973_1"].ToString().Trim(); //登錄員
                            //*************到著處理
                            lblCheckUSER_ID.Text = ds.Tables["zdf631"].Rows[0]["F63155"].ToString().Trim(); //點貨人員
                            lblCheckUSER_NAME.Text = ds.Tables["zdf631"].Rows[0]["CHECK_USER_NAME"].ToString().Trim(); //點貨人員姓名
                            lblCheck_COUNT.Text = ds.Tables["zdf631"].Rows[0]["F63156"].ToString().Trim(); //點卸件數
                            lblBroken_COUNT.Text = ds.Tables["zdf631"].Rows[0]["F63158"].ToString().Trim(); //破損件數
                            lblCheck_date.Text = ds.Tables["zdf631"].Rows[0]["F63151"].ToString().Trim(); //點貨日
                            lblCheck_time.Text = ds.Tables["zdf631"].Rows[0]["F6315C"].ToString().Trim();  //點貨時間
                            //*************配送處理
                            lblDRIVER_Distr.Text = ds.Tables["zdf631"].Rows[0]["F6318A"].ToString().Trim(); //攜出人員
                            lblDRIVER_Distr_NAME.Text = ds.Tables["zdf631"].Rows[0]["Distr_NAME"].ToString().Trim(); //攜出人員姓名
                            lblDISTR_COUNT.Text = ds.Tables["zdf631"].Rows[0]["F63187"].ToString().Trim(); //件數
                            lblDISTR_DATE.Text = ds.Tables["zdf631"].Rows[0]["F6318B"].ToString().Trim(); //攜出日
                            lblDISTR_TIME.Text = ds.Tables["zdf631"].Rows[0]["F6318C"].ToString().Trim(); //時間
                            //*************配達處理
                            lblDRIVER_REACH.Text = ds.Tables["zdf631"].Rows[0]["F63167"].ToString().Trim(); //駕駛員
                            lblDRIVER_REACH_NAME.Text = ds.Tables["zdf631"].Rows[0]["DISPATCH_DRIVER_NAME"].ToString().Trim(); //駕駛員姓名
                            lblREACH_COUNT.Text = ""; //件數
                            lblREACH_DATE.Text = ds.Tables["zdf631"].Rows[0]["F63161"].ToString().Trim(); //銷卡日
                            lblREACH_TIME.Text = ds.Tables["zdf631"].Rows[0]["F6316C"].ToString().Trim(); //時間
                            //*************存庫處理
                            lblDRIVER_STOCK.Text = ds.Tables["zdf631"].Rows[0]["F63177"].ToString().Trim(); //駕駛員
                            lblDRIVER_STOCK_NAME.Text = ds.Tables["zdf631"].Rows[0]["STOCK_NAME"].ToString().Trim(); //駕駛員姓名
                            lbl_DAMAGE_COUNT_3.Text = ds.Tables["zdf631"].Rows[0]["STOCK_NAME"].ToString();//件數
                            lblSTOCK_DATE.Text = ds.Tables["zdf631"].Rows[0]["F63171"].ToString().Trim(); //存庫日
                            lblSTOCK_TIME.Text = ds.Tables["zdf631"].Rows[0]["F6317C"].ToString().Trim(); //時間

                            //地址
                            lblADDRESS.Text = ds.Tables["zdf631"].Rows[0]["F21703"].ToString().Trim(); ;
                            //更正原因
                            lblREASON.Text = ds.Tables["zdf631"].Rows[0]["F21704"].ToString().Trim(); ;

                            //查詢或傳真簽收單
                            lblSHIPPING_NO_2.Text = ds.Tables["zdf631"].Rows[0]["F631A1"].ToString().Trim(); //托運單號
                            lblSHIPPER_2.Text = ds.Tables["zdf631"].Rows[0]["F21526"].ToString().Trim(); //更正後-托運人
                            lblRECEIVER_2.Text = ds.Tables["zdf631"].Rows[0]["F21527"].ToString().Trim(); //更正後-收貨人
                            //txtFAX_NO.Text = ds.Tables["zdf631"].Rows[0]["CS4521"].ToString().Trim(); //傳真號碼
                            //txtAddressee.Text = ds.Tables["zdf631"].Rows[0]["CS4524"].ToString().Trim(); //收件人
                            //lblLOGDATE.Text = ds.Tables["zdf631"].Rows[0]["CS4512"].ToString().Trim() + ' ' + ds.Tables["zdf631"].Rows[0]["CS4513"].ToString().Trim(); ; //登錄日
                            //lblLOGUSER.Text = ds.Tables["zdf631"].Rows[0]["CS4514"].ToString().Trim(); //登錄人

                            //srcImageUrl = "http://www.ddsc.com.tw/images/main/mainpic.jpg";
                            srcImageUrl = "http://" + ds.Tables["zdf631"].Rows[0]["DCE702"].ToString().Trim() + "/" + ds.Tables["zdf631"].Rows[0]["DCE703"].ToString().Trim();
                            hidImage_URL.Value = srcImageUrl;

                            if (srcImageUrl.Trim() != "")
                            {
                                //Image1.ImageUrl = srcImageUrl;

                                ////儲存圖片在Server上
                                ////this.saveThumbPic(srcImageUrl, 100, Server.MapPath("~/test.jpg"));
                                //System.Drawing.Image.GetThumbnailImageAbort callBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
                                ////取得原始圖片
                                //image = this.getImageFromURL(srcImageUrl);

                                //ltImage.Text = string.Format(ltImage.Text, ImageToBase64(image, ImageFormat.Jpeg));
                                btnSEND.Enabled = true;
                            }
                            else
                            {
                                btnSEND.Enabled = false;
                            }

                            //路線貨件處理查詢
                            lblSHIPPING_NO_3.Text = ds.Tables["zdf631"].Rows[0]["F631A1"].ToString().Trim(); //託運單號
                            lblCANCEL_3.Text = ds.Tables["zdf631"].Rows[0]["f20602_1"].ToString().Trim(); ;//銷卡
                            lblSTOCK_3.Text = ds.Tables["zdf631"].Rows[0]["f20602_2"].ToString().Trim(); ; //存庫

                            //照會資料查詢
                            lblSHIPPING_NO_4.Text = ds.Tables["zdf631"].Rows[0]["F631A1"].ToString().Trim(); //託運單號

                        }
                        else
                        {
                            lbl_message.Text = "明細單號:" + previous.g_GridKey["DETAIL_NO"].ToString() + " 貨號:" + previous.g_GridKey["ITEM_NO"].ToString() + " 資料讀取錯誤！";
                        }

                    }
                    catch (Exception ex)
                    {
                        lbl_message.Text = ex.Message;
                    }


                }

            }
            else
            {
                Image1.ImageUrl = hidImage_URL.Value;
            }
            //執行搜尋功能
            ClientScript.RegisterClientScriptBlock(this.GetType(), "btnSearch_Click", "<script language='javascript'>$(document).ready(function () {btnSearch_Click(''); }) </script>");
        }
        catch (Exception ex)
        {
            //Response.Write(e.Message);
            
            lbl_message.Text = BP_Message;
            ERROR_DBSAVE("krj_tracking_detail.aspx.cs", "Page_Load", ex.Message, "", "c#", "0", ex.StackTrace);

        }
        
    }
    private string ImageToBase64(System.Drawing.Image image, System.Drawing.Imaging.ImageFormat format)
    {
        try
        {
            using (MemoryStream ms = new MemoryStream())
            {
                // Convert Image to byte[]
                image.Save(ms, format);
                byte[] imageBytes = ms.ToArray();

                // Convert byte[] to Base64 String
                string base64String = Convert.ToBase64String(imageBytes);
                return base64String;
            }
        }
        catch (Exception ex)
        {
            BasePermissionPage BP = new BasePermissionPage();
            lbl_message.Text = BP.BP_Message;
            BP.ERROR_DBSAVE("krj_tracking_detail.aspx.cs", "ImageToBase64", ex.Message, "", "c#", "0", ex.StackTrace);
            return "";
        }
    }
    


    #region 取得網路上的圖片
    /// <summary>
    /// 取得網路上的圖片
    /// </summary>
    /// <param name="strUrl">圖片的Url路徑</param>
    /// <returns>回傳 System.Drawing.Image物件</returns>
    public System.Drawing.Image getImageFromURL(string strUrl)
    {
        System.Drawing.Image MyImage = null;

        try
        {
            //建立一個 Web Request
            WebRequest MyWebRequest = WebRequest.Create(strUrl);
            //由 Web Request 取得 Web Response
            WebResponse MyWebResponse = MyWebRequest.GetResponse();
            //由 Web Response 取得 Stream
            Stream MyStream = MyWebResponse.GetResponseStream();
            //由 Stream 取得 Image
            MyImage = System.Drawing.Image.FromStream(MyStream);

            //該關的關一關, 該放的放一放
            MyStream.Close();
            MyStream.Dispose();
            MyWebResponse.Close();
            MyWebResponse = null;
            MyWebRequest = null;

        }
        catch (Exception ex)
        {
            //throw new Exception("getImageFromURL(string strUrl)發生例外，可能抓不到網路上的圖片" + strUrl);
            BasePermissionPage BP = new BasePermissionPage();
            lbl_message.Text = BP.BP_Message;
            BP.ERROR_DBSAVE("krj_tracking_detail.aspx.cs", "getImageFromURL", ex.Message, "", "c#", "0", ex.StackTrace);
        }

        //回傳 Image
        return MyImage;
    }

    #endregion


    #region [ASP.net程式使用]圖片等比例縮圖後的寬和高像素
    /// <summary>
    /// [ASP.net程式使用]取得圖片等比例縮圖後的寬和高像素
    /// </summary>
    /// <param name="image">System.Drawing.Image 的物件</param>
    /// <param name="maxPx">寬或高超過多少像素就要縮圖</param>
    /// <returns>回傳int陣列，索引0為縮圖後的寬度、索引1為縮圖後的高度</returns>
    public int[] getThumbPic_WidthAndHeight(System.Drawing.Image image, int maxPx)
    {

        int fixWidth = 0;

        int fixHeight = 0;

        if (image.Width > maxPx || image.Height > maxPx)
        //如果圖片的寬大於最大值或高大於最大值就往下執行 
        {

            if (image.Width >= image.Height)
            //圖片的寬大於圖片的高 
            {

                fixHeight = Convert.ToInt32((Convert.ToDouble(maxPx) / Convert.ToDouble(image.Width)) * Convert.ToDouble(image.Height));
                //設定修改後的圖高 
                fixWidth = maxPx;
            }
            else
            {

                fixWidth = Convert.ToInt32((Convert.ToDouble(maxPx) / Convert.ToDouble(image.Height)) * Convert.ToDouble(image.Width));
                //設定修改後的圖寬 
                fixHeight = maxPx;

            }



        }
        else
        {//圖片沒有超過設定值，不執行縮圖 

            fixHeight = image.Height;

            fixWidth = image.Width;

        }

        int[] fixWidthAndfixHeight = { fixWidth, fixHeight };



        return fixWidthAndfixHeight;
    }

    #endregion

    #region 產生縮圖並儲存
    /// <summary>
    /// 產生縮圖並儲存
    /// </summary>
    /// <param name="srcImageUrl">來源圖片的Url</param>
    /// <param name="maxPix">超過多少像素就要等比例縮圖</param>
    /// <param name="saveThumbFilePath">縮圖的儲存檔案路徑</param>
    public void saveThumbPic(string srcImageUrl, int maxPix, string saveThumbFilePath)
    {
        //為了callBack而callBack的寫法
        System.Drawing.Image.GetThumbnailImageAbort callBack =
             new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);


        //取得原始圖片
        System.Drawing.Image image = this.getImageFromURL(srcImageUrl);
        // 計算維持比例的縮圖大小
        int[] thumbnailScale = this.getThumbPic_WidthAndHeight(image, maxPix);
        // 產生縮圖
        System.Drawing.Image smallImage =
        image.GetThumbnailImage(thumbnailScale[0], thumbnailScale[1], callBack, IntPtr.Zero);
        // 將縮圖存檔
        smallImage.Save(saveThumbFilePath);
        // 釋放
        image.Dispose();
    }
    private bool ThumbnailCallback()
    {
        return false;
    }
    #endregion



    protected void btnSEND_Click(object sender, EventArgs e)
    {
        try
        {
            string srcImageUrl = "";
            srcImageUrl = hidImage_URL.Value;
            //取得原始圖片
            MemoryStream memoryStream = null;
            LinkedResource imagelink = null;
            WebClient client = new WebClient();
            byte[] myDataBuffer = client.DownloadData(srcImageUrl);
            memoryStream = new MemoryStream(myDataBuffer);
            imagelink = new LinkedResource(memoryStream, client.ResponseHeaders["Content-Type"]);
            imagelink.ContentId = "image1";
            imagelink.ContentLink = new Uri("cid:" + "image1");
            imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

            string str = "<html><body><h1>親愛的客戶您好:</h1><br/><br/><h1>您查詢的簽收單如下圖，如有任何疑問請電洽營業所為您服務，謝謝!<h1><br/><img src=\"cid:image1\"></body></html>";
            AlternateView av = AlternateView.CreateAlternateViewFromString(str, null, MediaTypeNames.Text.Html);
            av.LinkedResources.Add(imagelink);
            MailUtil.SendMailWithAttachment("service@kerrytj.com", txtEMAIL.Text.Trim(), "[Kerry Web System]簽收單圖片傳送", "簽收單圖片傳送", av,false);

        }
        catch (Exception ex)
        {
            //lbl_message.Text = ex.Message;
            BasePermissionPage BP = new BasePermissionPage();
            lbl_message.Text = BP.BP_Message;
            BP.ERROR_DBSAVE("krj_tracking_detail.aspx.cs", "btnSEND_Click", ex.Message, "", "c#", "0", ex.StackTrace);
        }
    }

    protected DataSet DBINT(string DETAIL_NO, string ITEM_NO, string CORRECT_NO, string ORG_SHIPPING_NO, string History)
    {
        DataSet ds = null;
        try
        {
            dbAccess m_dba = new dbAccess();
            StringBuilder m_sb = new StringBuilder();
            ArrayList m_arrParam = new ArrayList();

            m_sb.Clear();
            m_sb.Append(" select Z.*,D.DEPT_NAME DISPATCH_DEPT  ,E.DEPT_NAME RECEIVED_DEPT,F.DEPT_NAME CORRECT_DEPT ");
            m_sb.Append(" , G.CS4521,G.CS4524,G.CS4512,G.CS4513,G.CS4514 ");
            m_sb.Append(" , nvl(H.user_name,' ') CHECK_USER_NAME ,nvl(I.user_name,' ') DISPATCH_DRIVER_NAME , nvl(J.user_name,' ') STOCK_NAME ,nvl(M.user_name,' ') Distr_NAME ");
            m_sb.Append(" , K.DCE702, K.DCE703 ");
            m_sb.Append(" , N.f20602 f20602_1, O.f20602 f20602_2 ");
            m_sb.Append(" , R.USER_NAME F61971_1, S.USER_NAME F61973_1 ");
            m_sb.Append(" from ( ");
            m_sb.Append(" select f63100 "); //資料識別
            m_sb.Append(" ,f63101"); //發送明細單號
            m_sb.Append(" ,f63102"); //貨號
            m_sb.Append(" ,f63101 || f63102 Detail_NO ");
            m_sb.Append(" , nvl(f63162,' ') f63162 "); //銷卡類別
            m_sb.Append(" , nvl(f63172,' ') f63172"); //存庫類別

            m_sb.Append(" ,nvl(f63103,' ') f63103 "); //發送日期
            m_sb.Append(" ,nvl(F63105,' ') F63105 "); //到著日期
            m_sb.Append(" ,nvl(F631B4,' ') F631B4 "); //出貨單號
            m_sb.Append(" ,nvl(F631A1,' ') F631A1 "); //托運單號
            m_sb.Append(" ,nvl(F63107,' ') F63107 "); //指送日期
            m_sb.Append(" ,nvl(F631B3,' ') F631B3 "); //行銷專案
            m_sb.Append(" ,nvl(F631C1,' ') F631C1 "); //指送時段1早上，2下午，3晚上，4不指定
            m_sb.Append(" ,nvl(F63140,0) F63140 "); //小計
            m_sb.Append(" ,nvl(F631B5,' ') F631B5 "); //加成識別

            m_sb.Append(" ,nvl(F61977,' ') F61977 "); //發送運輸單
            m_sb.Append(" ,nvl(F61971,' ') F61971 "); //受付員
            m_sb.Append(" ,nvl(F61973,' ') F61973 "); //登錄員
                        

            m_sb.Append(" ,nvl(F63141,0) F63141 "); //原訂代收款

            m_sb.Append(" , case when F21526 is null then nvl(F63111,' ') else F21526 end F21526 "); //更正-托運人
            m_sb.Append(" , case when F21504 is null then nvl(F63104,' ') else F21504 end F21504 "); //發送所站
            m_sb.Append(" , case when F21510 is null then nvl(F63106,' ') else F21510 end F21510 "); //原訂到著站所
            m_sb.Append(" , case when F21524 is null then nvl(F63106,' ') else F21524 end F21524 "); //更正後到著站所
            m_sb.Append(" , case when F2157D is null then nvl(F63117,' ') else F2157D end F63117 "); //付款人編號

            m_sb.Append(" , case when F21532 is null then nvl(F63123,' ') else F21532 end F21532 "); //更正-支付
            m_sb.Append(" , case when F21533 is null then F63125 else F21533 end F21533 "); //更正-運費
            m_sb.Append(" , case when F21575 is null then F63131 else F21575 end F21575 "); //更正-報值費
            m_sb.Append(" , case when F21529 is null then F63119 else F21529 end F21529 "); //更正-件數
            m_sb.Append(" , case when F2157A is null then F63124 else F2157A end F2157A "); //更正-運選碼
            m_sb.Append(" , case when F21520 is null then F63126 else F21534 end F21520 "); //更正-接費
            m_sb.Append(" , case when F21530 is null then F63120 else F21530 end F21530 "); //更正-重量
            m_sb.Append(" , case when F21579 is null then F63142 else F21579 end F21579 "); //更正-報值額
            m_sb.Append(" , case when F21534 is null then F63126 else F21534 end F21534 "); //更正-接費
            m_sb.Append(" , case when F21535 is null then F63128 else F21535 end F21535 "); //更正-送費
            m_sb.Append(" , case when F21574 is null then F63132 else F21574 end F21574 "); //更正-特服費
            m_sb.Append(" , case when F21531 is null then F63121 else F21531 end F21531 "); //更正-才數
            m_sb.Append(" , case when F21572 is null then nvl(F63143,' ') else F21572 end F21572 "); //更正-特服碼1
            m_sb.Append(" , case when F21536 is null then F63130 else F21536 end F21536 "); //更正-代款

            m_sb.Append(" , F63127 "); //更正-接捆工費
            m_sb.Append(" , F63128 "); //更正-送捆工費
            m_sb.Append(" , F63133 "); //更正-假日費


            //m_sb.Append(" , case when F2156F is null then F63141 else to_number(F2156F) end F2156F "); //原訂-代收貨款
            m_sb.Append(" , nvl(F21509,' ') F21509 "); //更正日期
            m_sb.Append(" , case when F21527 is null then F63114 else nvl(F21527,' ') end F21527 "); //更正-收貨人
            m_sb.Append(" , nvl(F21501,' ') F21501 "); //更正單號
            m_sb.Append(" , nvl(F21508,' ') F21508 "); //更正所站

            m_sb.Append(" ,F631A5"); //登錄員
            m_sb.Append(" ,F63155"); //點貨人員
            m_sb.Append(" ,F63156"); //點卸件數
            m_sb.Append(" ,F63158"); //破損件數
            m_sb.Append(" ,F63187"); //件數
            m_sb.Append(" ,F6318B"); //攜出日
            m_sb.Append(" ,F6318C"); //時間
            m_sb.Append(" ,F63167"); //配達-銷卡駕駛員
            m_sb.Append(" ,F63164"); //配達-銷卡件數
            m_sb.Append(" ,nvl(F63161,0) F63161 "); //配達-銷卡日期
            m_sb.Append(" ,nvl(F6316C,' ') F6316C "); //配達-銷卡時間
            m_sb.Append(" ,F63165"); //配達-銷卡運輸單號
            m_sb.Append(" ,F63166"); //配達-車輛編號
            m_sb.Append(" ,F63177"); //存庫駕駛員
            m_sb.Append(" ,nvl(F63171,' ') F63171 "); //存庫日期
            m_sb.Append(" ,nvl(F6317C,0) F6317C "); //存庫時間
            m_sb.Append(" ,F63185"); //攜出運輸單號
            m_sb.Append(" ,F6318A"); //攜出登錄人員
            m_sb.Append(" ,nvl(F63151,' ') F63151 "); //點貨登錄日期
            m_sb.Append(" ,nvl(F6315C,0) F6315C "); //點貨登錄時間

            m_sb.Append(" , case when Q.F21703 is null then NVL(f61711,' ') else Q.F21703 end F21703 "); //地址
            m_sb.Append(" , (NVL(Q.F21704,' ') || NVL(Q.F21705,' ') || NVL(Q.F21706,' ') || NVL(Q.F21707,' ') || NVL(Q.F21708,' ') || NVL(Q.F21709,' ')  ) F21704 "); //原因

            m_sb.Append(" from (select * from AS400_ZDF631 Where F63101 = :DETAIL_NO AND f63102 = :ITEM_NO ) B ");
            m_arrParam.Add(m_dba.GetOracleParam(":DETAIL_NO", DETAIL_NO));
            m_arrParam.Add(m_dba.GetOracleParam(":ITEM_NO", ITEM_NO));
            m_sb.Append(" left join (select Z.* from as400_zdf215 Z where Z.F21501 = :CORRECT_NO ) C on B.F63101 = C.F21502 and B.F63102 = C.F21503  ");
            m_arrParam.Add(m_dba.GetOracleParam(":CORRECT_NO", CORRECT_NO));
            m_sb.Append(" left join (select Z.F61901,Z.F61902,Z.F61903,Z.F61904, Z.F61977,Z.F61971,Z.F61973,Z.F619Z1 from as400_zdf619 Z where Z.F619Z1 = :ORG_SHIPPING_NO ");
            m_arrParam.Add(m_dba.GetOracleParam(":ORG_SHIPPING_NO", ORG_SHIPPING_NO));
            if (CORRECT_NO != "")
            {
                m_sb.Append(" and Z.F61904 = :CORRECT_NO_1 "); //已改過更正單
                m_arrParam.Add(m_dba.GetOracleParam(":CORRECT_NO_1", CORRECT_NO));
            }
            else
            {
                m_sb.Append(" and Z.F61904 is null "); //原託運單
            }
            m_sb.Append(" ) L on B.F631A1 = L.F619Z1  ");
            m_sb.Append(" left join (select f61702, f61711 from as400_zdf617 where f61702 = :ORG_SHIPPING_NO_1 ) P on B.F631Z1 = P.f61702  ");
            m_arrParam.Add(m_dba.GetOracleParam(":ORG_SHIPPING_NO_1", DETAIL_NO + ITEM_NO));

            m_sb.Append(" left join (select f21702, F21703, F21704, F21705, F21706, F21707, F21708, F21709 from as400_zdf217 where f21702 = :CORRECT_NO_2) Q on L.F61904 = Q.f21702  ");
            m_arrParam.Add(m_dba.GetOracleParam(":CORRECT_NO_2", CORRECT_NO));

            m_sb.Append(" ) Z ");
            m_sb.Append(" left join mis_dept_ap D on Z.F21504 = D.DEPT_ID ");//發送所站
            m_sb.Append(" left join mis_dept_ap E on Z.F21510 = E.DEPT_ID ");//原訂到著所站
            m_sb.Append(" left join mis_dept_ap F on Z.F21524 = F.DEPT_ID ");//更正後所站
            m_sb.Append(" left join as400_CSF45 G on Z.F631A1 = G.CS4501 ");//簽收單
            m_sb.Append(" left join (select distinct DCE701,DCE702,first_value(DCE703) over (partition by DCE701 order by DCE791 ,DCE792 desc )  AS DCE703 from as400_dcfe7) K on Z.F631A1 = K.DCE701 ");//簽收單圖片
            m_sb.Append(" left join mis_user_ap H on to_char(Z.F63155) = H.user_id ");//點貨人員
            m_sb.Append(" left join mis_user_ap I on to_char(Z.F63167) = I.user_id ");//配達處理-駕駛員
            m_sb.Append(" left join mis_user_ap J on to_char(Z.F63177) = J.user_id ");//存庫處理-駕駛員
            m_sb.Append(" left join mis_user_ap M on to_char(Z.F6318A) = M.user_id ");//攜出登錄人員
            m_sb.Append(" left join as400_ZDF206 N on Z.F63162 = N.F20601 ");//攜出登錄人員
            m_sb.Append(" left join as400_ZDF206 O on Z.F63172 = O.F20601 ");//攜出登錄人員
            m_sb.Append(" left join mis_user_ap R on Z.F61971 = R.USER_ID ");//受付員
            m_sb.Append(" left join mis_user_ap S on Z.F61973 = S.USER_ID ");//登錄員

            if (History.ToLower() == "true")
            {
                m_sb.Replace("AS400_ZDF631", "AS400_ZDF631Z");
            }
                       
            ds = m_dba.runExecuteQuery("zdf631", m_sb.ToString(), m_arrParam);
            

        }
        catch (Exception ex)
        {
            
            lbl_message.Text = BP_Message;
            ERROR_DBSAVE("krj_tracking_detail.aspx.cs", "DBINT", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return ds;
    }
    protected Boolean chk_F631(string CORRECT_NO, string ITEM_NO, string History)
    {
        DataSet ds = null;
        try
        {
            dbAccess m_dba = new dbAccess();
            StringBuilder m_sb = new StringBuilder();
            ArrayList m_arrParam = new ArrayList();
            m_sb.Clear();
            m_sb.Append(" select F63101 FROM AS400_ZDF631 A  ");
            m_sb.Append(" Where F63101 = :CORRECT_NO ");
            m_sb.Append(" AND f63102 = :ITEM_NO ");
            m_arrParam.Add(m_dba.GetOracleParam(":CORRECT_NO", CORRECT_NO));
            m_arrParam.Add(m_dba.GetOracleParam(":ITEM_NO", ITEM_NO));
            if (History.ToLower() == "true")
            {
                m_sb.Replace("AS400_ZDF631", "AS400_ZDF631Z");
            }
            ds = m_dba.runExecuteQuery("zdf631", m_sb.ToString(), m_arrParam);

            if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "000" && ds.Tables["zdf631"].Rows.Count == 1)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        catch (Exception ex)
        {
            
            lbl_message.Text = BP_Message;
            ERROR_DBSAVE("krj_tracking_detail.aspx.cs", "chk_F631", ex.Message, "", "c#", "0", ex.StackTrace);
            return false;
        }
        
    }

    protected DataSet chk_F262(string CHECKING_NO, string CHECKING_SEQ)
    {
        DataSet ds = null;
        try
        {
            dbAccess m_dba = new dbAccess();
            StringBuilder m_sb = new StringBuilder();
            ArrayList m_arrParam = new ArrayList();
            m_sb.Clear();
            m_sb.Append(" select A.F26203 || ' ' || B.DEPT_NAME F26203,A.F26225 || ' ' || C.USER_NAME F26225, NVL(A.F26205,' ') || ' ' ||  NVL(A.F26206,' ') F26205  ");
            m_sb.Append(" , A.F26207 || ' ' || D.DEPT_NAME F26207,A.F26239 || ' ' || E.USER_NAME F26239, NVL(A.F26240,' ') || ' ' ||  NVL(A.F26241,' ') F26240,NVL(A.F26223,' ') F26223 ");
            m_sb.Append(" , A.F26203 || ' ' || B.DEPT_NAME F26203_1,A.F26230 || ' ' || F.USER_NAME F26230, NVL(A.F26231,' ') || ' ' ||  NVL(A.F26232,' ') F26231,NVL(A.F26224,' ') F26224 ");
            m_sb.Append(" , NVL(A.F26208,' ') F26208,NVL(A.F26235,' ') F26235 ");
            m_sb.Append(" , NVL(H.F26304_1,' ') F26304_1, NVL(H.F26304_2,' ') F26304_2, NVL(H.F26304_3,' ') F26304_3, NVL(H.F26304_4,' ') F26304_4, NVL(H.F26304_5,' ') F26304_5, NVL(H.F26304_6,' ') F26304_6");
            m_sb.Append(" , NVL(G.F26404_1,' ') F26404_1, NVL(G.F26404_2,' ') F26404_2, NVL(G.F26404_3,' ') F26404_3, NVL(G.F26404_4,' ') F26404_4, NVL(G.F26404_5,' ') F26404_5, NVL(G.F26404_6,' ') F26404_6");
            m_sb.Append(" , A.F26215 || ' ' || I.DEPT_NAME F26215,A.F26233 || ' ' || J.DEPT_NAME F26233,A.F26209,A.F26210,A.F26211,A.F26212,A.F26213");
            m_sb.Append(" from as400_zdf262 A left join MIS_DEPT_AP B on A.F26203 = B.DEPT_ID");
            m_sb.Append(" left join MIS_USER_AP C on A.F26225 = C.USER_ID");
            m_sb.Append(" left join MIS_DEPT_AP D on A.F26207 = D.DEPT_ID");
            m_sb.Append(" left join MIS_USER_AP E on A.F26239 = E.USER_ID");
            m_sb.Append(" left join MIS_USER_AP F on A.F26230 = F.USER_ID");
            m_sb.Append(" left join MIS_DEPT_AP I on A.F26215 = I.DEPT_ID");
            m_sb.Append(" left join MIS_DEPT_AP J on A.F26233 = J.DEPT_ID");

            m_sb.Append(" left join (select F26301,F26302,MAX(F26304_1) F26304_1,MAX(F26304_2) F26304_2,MAX(F26304_3) F26304_3,MAX(F26304_4) F26304_4,MAX(F26304_5) F26304_5,MAX(F26304_6) F26304_6");
            m_sb.Append(" from");
            m_sb.Append(" (");
            m_sb.Append(" select F26301,F26302");
            m_sb.Append(" , decode(F26303,'1',F26304,'') F26304_1");
            m_sb.Append(" , decode(F26303,'2',F26304,'') F26304_2 ");
            m_sb.Append(" , decode(F26303,'3',F26304,'') F26304_3");
            m_sb.Append(" , decode(F26303,'4',F26304,'') F26304_4");
            m_sb.Append(" , decode(F26303,'5',F26304,'') F26304_5");
            m_sb.Append(" , decode(F26303,'6',F26304,'') F26304_6");
            m_sb.Append(" from AS400_ZDF263");
            m_sb.Append(" WHERE F26301 = :CHECKING_NO_1 and F26302 = :CHECKING_SEQ_1  ");
            m_sb.Append(" ) Z Group BY  F26301,F26302");
            m_sb.Append(" ) H on A.F26201 = H.F26301 and A.F26202 = H.F26302 ");

            m_sb.Append(" left join (select F26401,F26402,MAX(F26404_1) F26404_1,MAX(F26404_2) F26404_2,MAX(F26404_3) F26404_3,MAX(F26404_4) F26404_4,MAX(F26404_5) F26404_5,MAX(F26404_6) F26404_6");
            m_sb.Append(" from");
            m_sb.Append(" (");
            m_sb.Append(" select F26401,F26402");
            m_sb.Append(" , decode(F26403,'1',F26404,'') F26404_1");
            m_sb.Append(" , decode(F26403,'2',F26404,'') F26404_2");
            m_sb.Append(" , decode(F26403,'3',F26404,'') F26404_3");
            m_sb.Append(" , decode(F26403,'4',F26404,'') F26404_4");
            m_sb.Append(" , decode(F26403,'5',F26404,'') F26404_5");
            m_sb.Append(" , decode(F26403,'6',F26404,'') F26404_6");
            m_sb.Append(" from AS400_ZDF264 ");
            m_sb.Append(" WHERE F26401 = :CHECKING_NO_2 and F26402 = :CHECKING_SEQ_2 ");
            m_sb.Append(" ) Z Group BY  F26401,F26402");
            m_sb.Append(" ) G on A.F26201 = G.F26401 and A.F26202 = G.F26402");
            m_sb.Append(" WHERE A.F26201 = :CHECKING_NO_3 and F26202 = :CHECKING_SEQ_3 ");


            m_arrParam.Add(m_dba.GetOracleParam(":CHECKING_NO_1", CHECKING_NO));
            m_arrParam.Add(m_dba.GetOracleParam(":CHECKING_SEQ_1", CHECKING_SEQ));
            m_arrParam.Add(m_dba.GetOracleParam(":CHECKING_NO_2", CHECKING_NO));
            m_arrParam.Add(m_dba.GetOracleParam(":CHECKING_SEQ_2", CHECKING_SEQ));
            m_arrParam.Add(m_dba.GetOracleParam(":CHECKING_NO_3", CHECKING_NO));
            m_arrParam.Add(m_dba.GetOracleParam(":CHECKING_SEQ_3", CHECKING_SEQ));

            ds = m_dba.runExecuteQuery("zdf262", m_sb.ToString(), m_arrParam);

            

        }
        catch (Exception ex)
        {
            lbl_message.Text = BP_Message;
            ERROR_DBSAVE("krj_tracking_detail.aspx.cs", "chk_F262", ex.Message, "", "c#", "0", ex.StackTrace);

        }
        return ds;
    }
}
